<template>
  <div class="hm_register">
    <van-nav-bar title="面经注册" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          square
          block
          type="info"
          color="#07C160"
          native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
    <router-link class="login" to="/login">有账号,去登录</router-link>
  </div>
</template>

<script>
import { registerApi } from "@/api/user";
export default {
  name: "HmRegister",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  // http://interview-api-t.itheima.net/h5/user/register
  methods: {
    async onSubmit(values) {
      await registerApi(values);

      // 注册成功后,写一个成功的提示
      this.$toast.success("注册成功");
      // 注册成功后跳转到登录页面
      this.$router.push("/login");
    },
  },
};
</script>

<style scoped lang="less">
.hm_register {
  .login {
    color: #069;
    font-size: 12px;
    padding-right: 20px;
    float: right;
  }
}
</style>
